package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysInformation;
import com.ruoyi.system.domain.SysProjectAlarm;
import com.ruoyi.system.service.SysAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysBoardSuperviseMapper;
import com.ruoyi.system.domain.SysBoardSupervise;
import com.ruoyi.system.service.ISysBoardSuperviseService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-22
 */
@Service
public class SysBoardSuperviseServiceImpl implements ISysBoardSuperviseService {
    @Autowired
    private SysBoardSuperviseMapper sysBoardSuperviseMapper;
    @Autowired
    private SysAlarmService sysAlarmService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysBoardSupervise selectSysBoardSuperviseById(Long id) {
        return sysBoardSuperviseMapper.selectSysBoardSuperviseById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysBoardSupervise 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysBoardSupervise> selectSysBoardSuperviseList(SysBoardSupervise sysBoardSupervise) {
        List<SysBoardSupervise> superviseList = sysBoardSuperviseMapper.selectSysBoardSuperviseList(sysBoardSupervise);
        Iterator<SysBoardSupervise> it = superviseList.iterator();
        List list = new ArrayList<>();
        while (it.hasNext()) {
            SysBoardSupervise next = it.next();
            if (StringUtils.isNotEmpty(next.getProjecter())) {
                SysProjectAlarm sysProjectAlarm = new SysProjectAlarm();
                String sss = next.getProjecter();
                List<Long> collect = Arrays.stream(sss.split(",")).map(Long::valueOf).collect(Collectors.toList());
                next.setSsss(collect);
                if (next.getEnd() != null) {
                    Date endTime = next.getEnd();
                    Date date = new Date();
                    int days = (int) ((date.getTime() - endTime.getTime()) / (1000 * 3600 * 24));
                    if (days > 5 && !"100".equals(next.getProjectProgress())) {
                        next.setColor("#F56C6C");
                        sysProjectAlarm.setTitle(next.getTitle());
                        sysProjectAlarm.setProjectProgress(next.getProjectProgress());
                        sysProjectAlarm.setOverTime(days);
                        sysProjectAlarm.setProgressId(next.getId());
//                        插入或更新
                        sysAlarmService.insertAlarmInformation(sysProjectAlarm);
                    } else {
                        next.setColor("#409EFF");
                        sysAlarmService.deleteAlarmInformation(next.getId());
                    }
                }
            }
            list.add(next);
        }
        return list;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysBoardSupervise 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysBoardSupervise(SysBoardSupervise sysBoardSupervise) {
        return sysBoardSuperviseMapper.insertSysBoardSupervise(sysBoardSupervise);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysBoardSupervise 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysBoardSupervise(SysBoardSupervise sysBoardSupervise) {
        return sysBoardSuperviseMapper.updateSysBoardSupervise(sysBoardSupervise);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysBoardSuperviseByIds(Long[] ids) {
        return sysBoardSuperviseMapper.deleteSysBoardSuperviseByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysBoardSuperviseById(Long id) {
        return sysBoardSuperviseMapper.deleteSysBoardSuperviseById(id);
    }
}
